package cn.t.utils;

import org.apache.commons.compress.utils.Lists;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

public class ExcelImportValidUtil {
    /**
     * Excel导入字段校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     */
    public static List<String> valid(Object object) throws Exception {
        List<String> stringList = Lists.newArrayList();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = field.get(object);
            //是否包含必填校验注解
            boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
            if (isExcelValid && Objects.isNull(fieldValue)) {
                ExcelValid excelValid = field.getAnnotation(ExcelValid.class);
                stringList.add(excelValid.name() + ":" + excelValid.msg());
            }
        }
        return stringList;
    }

}
